﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using TSCC.DataAccess;

namespace TSCC.Service
{
    public enum ReportCategory
    {
        Summary1, Summary2, Detail1, Detail2
    }

    public class Service_Report
    {
        protected List<DataRow[]> itemAB = null;

        /// <summary>
        /// 總表1
        /// </summary>
        /// <returns></returns>
        public DataTable GetReportData(List<string>p_dateList
                                ,List<string>p_itemList
                                ,List<string>p_team
                                ,ReportCategory p_category)
        {
            DataTable dt = null;
            if (p_category == ReportCategory.Summary1)
            {
                dt = getSummaryData1(p_dateList, p_itemList, p_team);
            }
            if (p_category == ReportCategory.Summary2)
            {
                dt = getSummaryData2_1(p_dateList, p_itemList, p_team);
            }
            if (p_category == ReportCategory.Detail1)
            {
                dt = getDetailData1(p_dateList, p_team);   
            }
            if (p_category == ReportCategory.Detail2)
            {
                dt = getDetailData2(p_dateList, p_team);
            }
            return dt;
        }

        public bool EndDateGreatThanStart(string p_date1,string p_date2)
        {
            bool ok = false;
            try
            {
                if (Utility.IsDate(p_date1) & Utility.IsDate(p_date2))
                {
                    ok = (Convert.ToDateTime(Utility.GetDateString(p_date1)) <=
                          Convert.ToDateTime(Utility.GetDateString(p_date2)))
                        ?true:false;
                }
            }
            catch (Exception ex)
            {
                ok = false;
            }
            return ok;
        }

        protected DataTable getSummaryData1(List<string> p_dateList
                                , List<string> p_itemList
                                , List<string> p_team)
        {
            DA_Report rpt = new DA_Report();
            bool fillWhere = false;
            string s1, s2;
            s1 = (string)p_dateList[0];
            s2 = (string)p_dateList[1];
            if (!EndDateGreatThanStart(s1, s2))
            {
                throw new Exception("日期錯誤");
            }

            List<string> newDateList = new List<string>();
            newDateList.Add(Utility.GetDateString(s1));
            newDateList.Add(Utility.GetDateString(s2));

            fillWhere = p_itemList.Count.Equals(0) ? false : true;

            if (p_itemList.Count.Equals(0))
            {
                List<TSCC.Interface.iCMBItem> newItems = Utility.GetExceptionList();
                foreach (CMBItem itm in newItems)
                {
                    p_itemList.Add(
                        Convert.ToString(itm.GetItemValue())
                        );
                }
            }

            rpt.AddItemCriteria(p_itemList, fillWhere);
            rpt.AddTeamCriteria(p_team);
            if (fillWhere == false)
            {
                p_itemList = new List<string>();
            }
            return rpt.GetSummaryData1(newDateList, p_itemList, p_team);
        }

        protected DataTable getSummaryData2(List<string> p_dateList
                               , List<string> p_itemList
                               , List<string> p_team)
        {
            
            DA_Report rpt = new DA_Report();
            bool fillWhere = false;
            string s1, s2;
            s1 = (string)p_dateList[0];
            s2 = (string)p_dateList[1];
            if (!EndDateGreatThanStart(s1, s2))
            {
                throw new Exception("日期錯誤");
            }

            List<string> newDateList = new List<string>();
            newDateList.Add(Utility.GetDateString(s1));
            newDateList.Add(Utility.GetDateString(s2));

            fillWhere = p_itemList.Count.Equals(0) ? false : true;

            if (p_itemList.Count.Equals(0))
            {
                List<TSCC.Interface.iCMBItem> newItems = Utility.GetExceptionList();
                foreach (CMBItem itm in newItems)
                {
                    p_itemList.Add(
                        Convert.ToString(itm.GetItemValue())
                        );
                }
            }

            rpt.AddItemCriteria(p_itemList, fillWhere);
            rpt.AddTeamCriteria(p_team);
            if (fillWhere == false)
            {
                p_itemList = new List<string>();
            }
            return rpt.GetSummaryData2(newDateList, p_itemList, p_team);
        }

        protected DataTable getSummaryData2_1(List<string> p_dateList
                               , List<string> p_itemList
                               , List<string> p_team)
        {
            DA_Report rpt = new DA_Report();
            fillListItemAB();
            
            bool fillWhere = false;
            string s1, s2;
            List<string> listItemA = new List<string>();
            List<string> listItemB = new List<string>();
            s1 = (string)p_dateList[0];
            s2 = (string)p_dateList[1];
            if (!EndDateGreatThanStart(s1, s2))
            {
                throw new Exception("日期錯誤");
            }

            List<string> newDateList = new List<string>();
            newDateList.Add(Utility.GetDateString(s1));
            newDateList.Add(Utility.GetDateString(s2));

            fillWhere = p_itemList.Count.Equals(0) ? false : true;

            if (p_itemList.Count.Equals(0))
            {
                List<TSCC.Interface.iCMBItem> newItems = Utility.GetExceptionList();
                foreach (CMBItem itm in newItems)
                {
                    if (checkItemIsA(Convert.ToString(itm.GetItemValue())))
                    {
                        listItemA.Add(Convert.ToString(itm.GetItemValue()));
                    }
                    else
                    {
                        listItemB.Add(Convert.ToString(itm.GetItemValue()));
                    }
                }
            }

            rpt.AddItemCriteria(listItemA, listItemB, fillWhere);
            rpt.AddTeamCriteria(p_team);
            if (fillWhere == false)
            {
                p_itemList = new List<string>();
            }
            DataTable dt = rpt.GetSummaryData2(newDateList, p_itemList, p_team);
            if (dt.Rows.Count > 0)
            {
                fillColumnCaption(dt);
            }
            return dt;
        }

        protected void fillColumnCaption(DataTable p_dt)
        {
            string[] itemACaption = { "回電時間", "結案筆數", "平均結案件數/時" };
            string[] itemBCaption = { "總提報時間","總提報次數","	平均時間"};
            string strCol;
            int x = 2;
            while (x < p_dt.Columns.Count)
            {
                strCol = p_dt.Columns[x].ToString().Replace("_1", "");
                fillColumnCaption(checkItemIsA(strCol) ? itemACaption:itemBCaption
                                    , p_dt, x);
                x = x + itemACaption.Length;
            }

        }

        protected void fillColumnCaption(string[] p_itemCaption, DataTable p_dt,int p_colPos)
        {
            for (int y = 0; y < p_itemCaption.Length; y++)
            {
                p_dt.Columns[p_colPos + y].Caption = p_itemCaption[y];
            }
        }

        protected DataTable getDetailData1(List<string> p_dateList                               
                               , List<string> p_team)
        {
            DA_Report rpt = new DA_Report();
            
            string s1, s2;
            s1 = (string)p_dateList[0];
            s2 = (string)p_dateList[1];
            if (!EndDateGreatThanStart(s1, s2))
            {
                throw new Exception("日期錯誤");
            }

            List<string> newDateList = new List<string>();
            newDateList.Add(Utility.GetDateString(s1));
            newDateList.Add(Utility.GetDateString(s2));

            
            rpt.AddTeamCriteria(p_team);

            return rpt.GetDetailData1(newDateList, p_team);
        }

        protected DataTable getDetailData2(List<string> p_dateList
                               , List<string> p_team)
        {
            DA_Report rpt = new DA_Report();

            string s1, s2;
            s1 = (string)p_dateList[0];
            s2 = (string)p_dateList[1];
            if (!EndDateGreatThanStart(s1, s2))
            {
                throw new Exception("日期錯誤");
            }

            List<string> newDateList = new List<string>();
            newDateList.Add(Utility.GetDateString(s1));
            newDateList.Add(Utility.GetDateString(s2));


            rpt.AddTeamCriteria(p_team);

            return rpt.GetDetailData2(newDateList, p_team);
        }

        protected void fillListItemAB()
        {
            DA_Exception obj = new DA_Exception();
            DataTable DT = obj.GetException();
            itemAB =new List<DataRow[]>();
            itemAB.Add(DT.Select("notempty like 'A*'"));
            itemAB.Add(DT.Select("notempty like 'B*'"));
        }

        protected bool checkItemIsA(string p_item)
        {
            bool find = false;
            int x = 0;
            foreach (DataRow[] row in itemAB)
            {
                for (int i = 0; i < row.Length; i++)
                {
                    if (row[i].ItemArray[0].ToString().Equals(p_item))
                    {
                        find = true;
                        break;
                    }
                }
                if (find == true)
                {
                    break;
                }
                x++;
            }
            find = x.Equals(0) ? true : false; //如果在0就找到表示是在A組中
            return find;
        }
    }
}
